﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Editform : Form
    {
        public Editform()
        {
            InitializeComponent();
        }

        public Editform(int id,string name ,string pwd)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = name;
            textBox2.Text = pwd;
        }

        private int id;

        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        //确认保存
        private void button1_Click(object sender, EventArgs e)
        {
            var pwdString = textBox2.Text.ToString();

            var name = textBox1.Text;
            var pwd = pwdString.Count() > 0 ? pwdString : "123456";

            if (id>0)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("update Users set Username = '{0}',Password='{1}' where Id = {2}", name, pwd,this.id);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("更新失败", "提示");
                        
                    }
                }
            }
            else
            {
                

                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("insert into Users (Username,Password) values ('{0}','{1}')",name,pwd);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示");
                        
                    }
                }
            }
        }

       
    }
}
